/**
 * 类db操作
 * 
 */

import com.google.appengine.api.datastore.*
import static com.google.appengine.api.datastore.FetchOptions.Builder.*


/**
 * @author sharl
 *
 */
class DBUtil{
	
	def datastore = DatastoreServiceFactory.getDatastoreService()
	
	def entity
	
	def query
	
	def deleteEntity(entityName,conditionName,cValue){
		
		entity = entireEntity(entityName,conditionName,cValue)
		
		datastore.withTransaction{
			entity.delete()
		}
	}
	
	def AddUser(userform){
		
		entity = new Entity("user")
		
		entity.passwd = userform["passwd"][0]
		entity.email = userform["email"][0]
		entity.alias = userform["alias"][0]
		entity.name = userform["name"][0]
		                               
		datastore.withTranscation{ entity.save() }
	}
	
	def entireEntity(entityName,conditionName,cValue){
		
		query = new Query(entityName)

		query.addFilter(conditionName, Query.FilterOperator.EQUAL, cValue) 

		PreparedQuery pq = datastore.prepare(query)

		return pq.asSingleEntity()
	}
	
	def SearchUser(){
		
		def query =new Query("user")
		query.addFilter("name",Query.FilterOperator.EQUAL , "aa")

		PreparedQuery pq = datastore.prepare(query)

		request['ret'] = pq.asList(withLimit(2));
	}
	
}
